package com.jilian.powerstation.modul.natives;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.os.Build;
import android.os.Handler;
import android.support.annotation.RequiresApi;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Base64;
import android.util.Log;
import android.view.View;

import com.jilian.powerstation.Constant;
import com.jilian.powerstation.R;
import com.jilian.powerstation.base.BaseActivity;
import com.jilian.powerstation.common.dto.WindowDto;
import com.jilian.powerstation.common.event.MessageEvent;
import com.jilian.powerstation.http.SocketConfig;
import com.jilian.powerstation.http.SocketUtils;
import com.jilian.powerstation.listener.CustomItemClickListener;
import com.jilian.powerstation.modul.adapter.WindowsAdapter;
import com.jilian.powerstation.utils.DataUtils;
import com.jilian.powerstation.utils.EmptyUtils;
import com.jilian.powerstation.utils.SPUtil;
import com.jilian.powerstation.utils.rxjava.RxBus;
import com.jilian.powerstation.utils.rxjava.RxBusBean;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import io.reactivex.functions.Consumer;

public class WindowActivity extends BaseActivity implements CustomItemClickListener {
    private WindowsAdapter adapter;
    private List<WindowDto> list;
    private RecyclerView recyclerview;
    private LinearLayoutManager linearLayoutManager;
    private NestedScrollView nestedScrollView;



    @Override
    protected void createViewModel() {

    }

    @Override
    public int intiLayout() {
        return R.layout.activity_window;
    }

    @Override
    public void initView() {

        setNormalTitle("Monitoring window", v -> finish());
        setrightTitle("save", "#000000", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                savePhoto(null,null,null,nestedScrollView);
            }
        });
        recyclerview = (RecyclerView) findViewById(R.id.recyclerview);
        linearLayoutManager = new LinearLayoutManager(this);
        recyclerview.setLayoutManager(linearLayoutManager);
        list = new ArrayList<>();
        adapter = new WindowsAdapter(list, this, this);
        recyclerview.setAdapter(adapter);
        nestedScrollView = (NestedScrollView) findViewById(R.id.nestedScrollView);

    }

    @Override
    public void initData() {
        getLoadingDialog().showDialog();
        EventBus.getDefault().register(this);

    }

    @Override
    public void initListener() {


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);

    }



    @Override
    public void onItemClick(View view, int position) {

    }


    /**
     * //监听外来是否要去成功的界面
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(MessageEvent event) {
        /* Do something */
        if (EmptyUtils.isNotEmpty(event)
                && EmptyUtils.isNotEmpty(event.getSendMessage())

        ) {

            initSenMessage(event.getSendMessage());
        }
    }

    private void initSenMessage(RxBusBean.SendMessage sendMessage) {
        Log.e(TAG, "WindowActivity接受到的数据: " + sendMessage.readContent.toString());
        Log.e(TAG, "fragment接受到的数据: " + sendMessage.readContent.toString());
        //车辆登入
        if (sendMessage != null && sendMessage.readContent[2] == SocketConfig.VEHICLE_ACCESS)
        {

            Log.e("车辆登入", "我接收到了的所有数据(已过滤传输规则中其他数据):" + Arrays.toString(sendMessage.info) + ",总数据长度:" + sendMessage.info.length);
            Log.e("车辆登入", "我接收到了来自车辆登入的接口数据");
            Log.e("车辆登入", "前六位是年月日时分秒:" + DataUtils.HexToInt(sendMessage.info[0]) + "年" + DataUtils.HexToInt(sendMessage.info[1]) + "月" + DataUtils.HexToInt(sendMessage.info[2]) + "日" + DataUtils.HexToInt(sendMessage.info[3]) + "时" + DataUtils.HexToInt(sendMessage.info[4]) + "分" + DataUtils.HexToInt(sendMessage.info[5]) + "秒");
            Log.e("车辆登入", "第七位和第八位是登入流水号:" + DataUtils.HexToInt(sendMessage.info[6]) + ":" + DataUtils.HexToInt(sendMessage.info[7]));
            StringBuilder stringBuffer = new StringBuilder("");
            for (int i = 0; i < 20; i++) {
                stringBuffer.append(sendMessage.info[8 + i]);
            }
            Log.e("车辆登入", "ICCID:" + stringBuffer.toString());
            Log.e("车辆登入", "动力蓄电池总数:" + sendMessage.info[28]);
            StringBuilder stringBuffer2 = new StringBuilder("");
            for (int i = 29; i < sendMessage.info.length; i++) {
                stringBuffer2.append(sendMessage.info[i]);
            }
            Log.e("车辆登入", "电池编码:" + stringBuffer2.toString());
            SPUtil.putData(Constant.SP_VALUE.SP, Constant.SP_VALUE.DEVICE_ID, SocketUtils.getStringBuilder(sendMessage.info));

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            Log.e("车辆登入", "发送的16进制指令: " + DataUtils.hexBytesToString(SocketConfig.getSetting()));
                            SocketUtils.sendSerialPort(SocketConfig.settingVehicleAccess(sendMessage.readContent));
                        }
                    }).start();

                }
            }, 1000);


        }

        if (sendMessage != null && sendMessage.readContent[2] == SocketConfig.HEARTBEAT)
        {

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            Log.e("回复心跳", "发送的16进制指令: " + DataUtils.hexBytesToString(SocketConfig.getSetting()));
                            SocketUtils.sendSerialPort(SocketConfig.settingVehicleAccess(sendMessage.readContent));
                        }
                    }).start();

                }
            }, 1000);




        }

        //   ***实时信息上报**
        if (sendMessage != null && sendMessage.readContent[2] == SocketConfig.REAL_TIME) {
            getLoadingDialog().dismiss();
            //ToastUitl.showImageToastTips("收到数据了");
            Log.e(TAG, "实时信息----------------+++++++++????" );
            Log.e("实时信息", "我接收到了实时信息的所有数据(已过滤传输规则中其他数据):" + Arrays.toString(sendMessage.info));
            Log.e("实时信息", "前六位是年月日时分秒:" + DataUtils.HexToInt(sendMessage.info[0]) + "年" + DataUtils.HexToInt(sendMessage.info[1]) + "月" + DataUtils.HexToInt(sendMessage.info[2]) + "日" + DataUtils.HexToInt(sendMessage.info[3]) + "时" + DataUtils.HexToInt(sendMessage.info[4]) + "分" + DataUtils.HexToInt(sendMessage.info[5]) + "秒");
            if (sendMessage.info.length > 7) {
                int length = DataUtils.HexToInt(sendMessage.info[8] + sendMessage.info[7]);//实时信息的数据长度
                if (DataUtils.HexToInt(sendMessage.info[6]) == 0x90) { //BSMU信息上传(0x90)

                    Log.e("实时信息0x90", ":" + Arrays.toString(sendMessage.info));
                    Log.e("实时信息0x90", "BSMU信息上传(0x90),对应数据长度:" + length + ",总数据长度:" + sendMessage.info.length);
                } else if (DataUtils.HexToInt(sendMessage.info[6]) == 0x91) {//BCMU信息上传(0x91)
                    Log.e("实时信息0x91", ":" + Arrays.toString(sendMessage.info));
                    Log.e("实时信息0x91", "BCMU信息上传(0x91),对应数据长度:" + length + ",总数据长度:" + sendMessage.info.length);
                    //电池功率 （BCMU电池组1运行功率 1W(负号表示充电)  ）
                    Log.e("实时信息", "XXXX电池功率（BCMU电池组1运行功率）=" + DataUtils.HexToInt(sendMessage.info[39] + sendMessage.info[38]));
                    //电池电压(电池组1总电压 U16 0.1V)
                    Log.e("实时信息", "XXXX电池电压（电池组1总电压）=" + DataUtils.HexToInt(sendMessage.info[35] + sendMessage.info[34]));
                    //电池电流(电池组1总电流,S16 0.1A(负号表示充电))
                    Log.e("实时信息", "XXXX电池电流（电池组1总电流）=" + DataUtils.HexToInt(sendMessage.info[37] + sendMessage.info[36]));
                    //电池功率
                    //
                    //剩余电量(电池组1剩余电能,1WH)
                    Log.e("实时信息", "XXXX剩余电量（电池组1剩余电能）=" + DataUtils.HexToInt(sendMessage.info[112] + sendMessage.info[111]));
                    //总电量(电池组1额定容量，1WH)
                    Log.e("实时信息", "XXXX总电量（电池组1额定容量）=" + DataUtils.HexToInt(sendMessage.info[110] + sendMessage.info[119]));

                    //剩余电量百分比
                    int all = DataUtils.HexToInt(sendMessage.info[110] + sendMessage.info[119]);
                    int has = DataUtils.HexToInt(sendMessage.info[112] + sendMessage.info[111]);
                    if (all != 0) {
                        double pe = has / all * 1.0;
                        Log.e("实时信息", "XXXX剩余电量百分比（剩余电量/总电量）=" + (pe * 100 + "%"));
                    } else {
                        Log.e("实时信息", "XXXX总电量为0了");
                        Log.e("实时信息", "XXXX剩余电量百分比（剩余电量/总电量）=" + (0 + "%"));
                    }
                    //电池温度(电池组平均温度 S16  1℃)
                    Log.e("实时信息", "XXXX电池温度（电池组平均温度）=" + DataUtils.HexToInt(sendMessage.info[164] + sendMessage.info[163]));

                } else if (DataUtils.HexToInt(sendMessage.info[6]) == 0x92) {//BMU信息上传(0x92)
                    Log.e("实时信息0x92", ":" + Arrays.toString(sendMessage.info));
                    Log.e("实时信息0x92", "BMU信息上传(0x92),对应数据长度:" + length + ",总数据长度:" + sendMessage.info.length);
                } else if (DataUtils.HexToInt(sendMessage.info[6]) == 0x93) {//PCS信息上传(0x93)
                    Log.e("实时信息0x93", ":" + Arrays.toString(sendMessage.info));
                    Log.e("实时信息0x93", "PCS信息上传(0x93),对应数据长度:" + length + ",总数据长度:" + sendMessage.info.length);
                    //EPS功率 （实时数据PCS负载A A相有功功率）
                    Log.e("实时信息", "XXXXEPS功率（实时数据PCS负载A A相有功功率）=" + DataUtils.HexToInt(sendMessage.info[143] + sendMessage.info[142]));
                    //负载功率（实时数据 PCS负载B A有相功率）
                    Log.e("实时信息", "XXXX负载功率（实时数据PCS负载B A相有功功率）=" + DataUtils.HexToInt(sendMessage.info[248] + sendMessage.info[247]));
                    //电网功率（实时数据 PCS A相有功功率（电网侧）  S16 1W(正号表示充电)）
                    Log.e("实时信息", "XXXX电网功率（实时数据 PCS A相有功功率（电网侧））=" + DataUtils.HexToInt(sendMessage.info[101] + sendMessage.info[100]));
                    //逆变器功率（A相有功功率（交流侧）  S16 1W(负号表示充电)
                    Log.e("实时信息", "XXXX逆变器功率（A相有功功率（交流侧））=" + DataUtils.HexToInt(sendMessage.info[59] + sendMessage.info[58]));
                    //逆变器告警
                    //光伏功率（PV1输入端功率+PV2输入端功率） S16 1W(负号表示充电)
                    int pv1 = DataUtils.HexToInt(sendMessage.info[188] + sendMessage.info[187]);
                    int pv2 = DataUtils.HexToInt(sendMessage.info[254] + sendMessage.info[253]);
                    Log.e("实时信息", "XXXX光伏功率（PV1输入端功率+PV2输入端功率）=" + (pv1 + pv2));

                    //1 PCS保护状态 PCS Protection status 1 位  3  9
                    int pcs_protection_status = DataUtils.HexToInt(sendMessage.info[9]);
                    //2 PCS工作状态
                    int pcs_working_status = DataUtils.HexToInt(sendMessage.info[10]);
                    //3 P CS工作模式 PCS working Mode
                    int pcs_working_mode = DataUtils.HexToInt(sendMessage.info[11]);
                    //4 电网状态 Grid Status
                    int grid_status = DataUtils.HexToInt(sendMessage.info[36]);
                    //5 电网继电器状态 Grid Replay Status
                    int grid_replay_status = DataUtils.HexToInt(sendMessage.info[37]);
                    //6 旁路继电器状态 Bypass relay status
                    int bypass_relay_status = DataUtils.HexToInt(sendMessage.info[38]);
                    //7 负载继电器状态 Load relay status
                    int load_relay_status = DataUtils.HexToInt(sendMessage.info[39]);

                    //8 A相电压（交流侧） Phase A voltage (AC side)
                    int phase_a_voltage  = DataUtils.HexToInt(sendMessage.info[41]+sendMessage.info[40]);
                    //9 B相电压（交流侧）Phase B voltage (AC side)
                    int phase_b_voltage  = DataUtils.HexToInt(sendMessage.info[43]+sendMessage.info[42]);
                    //10 C相电压（交流侧） Phase C voltage (AC side)
                    int phase_C_voltage  = DataUtils.HexToInt(sendMessage.info[45]+sendMessage.info[44]);

                    //11 A相频率（交流侧） Phase A frequency (AC side)
                    int phase_a_frequency  = DataUtils.HexToInt(sendMessage.info[47]+sendMessage.info[46]);
                    //12 B相频率（交流侧） Phase B frequency (AC side)
                    int phase_b_frequency  = DataUtils.HexToInt(sendMessage.info[49]+sendMessage.info[48]);
                    //13 C相频率（交流侧） Phase C frequency (AC side)
                    int phase_c_frequency  = DataUtils.HexToInt(sendMessage.info[51]+sendMessage.info[50]);



                    //14 A相电流（交流侧） Phase A current (AC side)
                    int phase_a_current   = DataUtils.HexToInt(sendMessage.info[53]+sendMessage.info[52]);
                    //15 B相电流（交流侧） Phase B current (AC side)
                    int phase_b_current  = DataUtils.HexToInt(sendMessage.info[55]+sendMessage.info[54]);
                    //16 C相电流（交流侧） Phase C current (AC side)
                    int phase_c_current  = DataUtils.HexToInt(sendMessage.info[57]+sendMessage.info[56]);


                    //17 A相有功功率（交流侧） Phase A active power (AC side)
                    int phase_a_active_power  = DataUtils.HexToInt(sendMessage.info[59]+sendMessage.info[58]);
                    //18 B相有功功率（交流侧） Phase B active power (AC side)
                    int phase_b_active_power  = DataUtils.HexToInt(sendMessage.info[61]+sendMessage.info[60]);
                    //19 C相有功功率（交流侧） Phase C active power (AC side)
                    int phase_c_active_power  = DataUtils.HexToInt(sendMessage.info[63]+sendMessage.info[62]);



                    //20 A相无功功率（交流侧） Phase A reactive power (AC side)
                    int phase_a_reactive_power  = DataUtils.HexToInt(sendMessage.info[65]+sendMessage.info[64]);
                    //21 B相无功功率（交流侧） Phase B reactive power (AC side)
                    int phase_b_reactive_power  = DataUtils.HexToInt(sendMessage.info[67]+sendMessage.info[66]);
                    //22 C相无功功率（交流侧） Phase C reactive power (AC side)
                    int phase_c_reactive_power  = DataUtils.HexToInt(sendMessage.info[69]+sendMessage.info[68]);


                    //23 A相视在功率（交流侧） Phase A apparent power (AC side)
                    int phase_a_apparent_power  = DataUtils.HexToInt(sendMessage.info[71]+sendMessage.info[70]);
                    //24 B相视在功率（交流侧） Phase B apparent power (AC side)
                    int phase_b_apparent_power  = DataUtils.HexToInt(sendMessage.info[73]+sendMessage.info[72]);
                    //25 C相视在功率（交流侧） Phase C apparent power (AC side)
                    int phase_c_apparent_power  = DataUtils.HexToInt(sendMessage.info[75]+sendMessage.info[74]);


                    //26 三相有功功率和（交流侧） The sum of three-phase active power (AC side)
                    int the_sum_of_three_phase_active_power  = DataUtils.HexToInt(sendMessage.info[77]+sendMessage.info[76]);
                    //27三相无功功率和（交流侧） The sum of three-phase reactive power (AC side)
                    int the_sum_of_three_phase_reactive_power  = DataUtils.HexToInt(sendMessage.info[79]+sendMessage.info[78]);
                    //28 三相视在功率和（交流侧）The sum of three-phase apparent power (AC side)
                    int the_sum_of_three_phase_apparent_power  = DataUtils.HexToInt(sendMessage.info[81]+sendMessage.info[80]);

                    //29 A相电压（电网侧） Phase A voltage (grid side)
                    int phase_a_voltage_grid_side  = DataUtils.HexToInt(sendMessage.info[83]+sendMessage.info[82]);
                    //30 B相电压（电网侧） Phase B voltage (grid side)
                    int phase_b_voltage_grid_side = DataUtils.HexToInt(sendMessage.info[85]+sendMessage.info[84]);
                    //31 C相电压（电网侧） Phase C voltage (grid side)
                    int phase_c_voltage_grid_side = DataUtils.HexToInt(sendMessage.info[87]+sendMessage.info[86]);



                    //32 A相频率（电网侧） Phase A frequency (grid side)
                    int phase_a_frequency_grid_side  = DataUtils.HexToInt(sendMessage.info[89]+sendMessage.info[88]);
                    //33 B相频率（电网侧） Phase B frequency (grid side)
                    int phase_b_frequency_grid_side  = DataUtils.HexToInt(sendMessage.info[91]+sendMessage.info[90]);
                    //34 C相频率（电网侧） Phase C frequency (grid side)
                    int phase_c_frequency_grid_side  = DataUtils.HexToInt(sendMessage.info[93]+sendMessage.info[92]);



                    //35 A相电流（电网侧） Phase A current (grid side)
                    int phase_a_current_grid_side  = DataUtils.HexToInt(sendMessage.info[95]+sendMessage.info[94]);
                    //36 B相电流（电网侧） Phase B current (grid side)
                    int phase_b_current_grid_side  = DataUtils.HexToInt(sendMessage.info[97]+sendMessage.info[46]);
                    //37 C相电流（电网侧） Phase C current (grid side)
                    int phase_c_current_grid_side  = DataUtils.HexToInt(sendMessage.info[99]+sendMessage.info[48]);


                    //38 A相有功功率（电网侧） Phase A active power (Grid side)
                    int phase_a_active_power_grid_side  = DataUtils.HexToInt(sendMessage.info[101]+sendMessage.info[100]);
                    //39 B相有功功率（电网侧） Phase B active power (Grid side
                    int phase_b_active_power_grid_side  = DataUtils.HexToInt(sendMessage.info[103]+sendMessage.info[102]);
                    //40 C相有功功率（电网侧） Phase C active power (Grid side)
                    int phase_c_active_power_grid_side  = DataUtils.HexToInt(sendMessage.info[105]+sendMessage.info[104]);



                    //41 A相无功功率（电网侧） Phase A reactive power (Grid side)
                    int phase_a_reactive_power_grid_side  = DataUtils.HexToInt(sendMessage.info[107]+sendMessage.info[106]);
                    //42 B相无功功率（电网侧） Phase B reactive power (Grid side)
                    int phase_b_reactive_power_grid_side  = DataUtils.HexToInt(sendMessage.info[109]+sendMessage.info[108]);
                    //43 C相无功功率（电网侧） Phase C reactive power (Grid side)
                    int phase_c_reactive_power_grid_side  = DataUtils.HexToInt(sendMessage.info[111]+sendMessage.info[110]);


                    //44 A相视在功率（电网侧） Phase A apparent power (Grid side)
                    int phase_a_apparent_power_grid_side  = DataUtils.HexToInt(sendMessage.info[113]+sendMessage.info[112]);
                    //45 B相视在功率（电网侧） Phase B apparent power (Grid side)
                    int phase_b_apparent_power_grid_side  = DataUtils.HexToInt(sendMessage.info[115]+sendMessage.info[114]);
                    //46 C相视在功率（电网侧） Phase C apparent power (Grid side)
                    int phase_c_apparent_power_grid_side  = DataUtils.HexToInt(sendMessage.info[117]+sendMessage.info[116]);

                    //47 三相有功功率和（电网侧） The sum of three-phase active power (Grid side)
                    int the_sum_of_three_phase_active_power_grid_side  = DataUtils.HexToInt(sendMessage.info[119]+sendMessage.info[118]);
                    //48 三相无功功率和（电网侧） The sum of three-phase reactive power (AC side)
                    int the_sum_of_three_phase_reactive_power_ac_side = DataUtils.HexToInt(sendMessage.info[121]+sendMessage.info[120]);
                    //49 三相无功功率和（电网侧） The sum of three-phase apparent power (AC side)
                    int the_sum_of_three_phase_apparent_power_ac_side  = DataUtils.HexToInt(sendMessage.info[123]+sendMessage.info[122]);


                    //50 A相电压（负载侧） Phase A voltage (Load side)
                    int phase_a_voltage_load_side  = DataUtils.HexToInt(sendMessage.info[125]+sendMessage.info[124]);
                    //51 B相电压（负载侧） Phase B voltage (Load side)
                    int phase_b_voltage_load_side = DataUtils.HexToInt(sendMessage.info[127]+sendMessage.info[126]);
                    //52 C相电压（负载侧） Phase C voltage (Load side)
                    int phase_c_voltage_load_side = DataUtils.HexToInt(sendMessage.info[129]+sendMessage.info[128]);



                    //53 A相频率（负载侧） Phase A frequency (Load side)
                    int phase_a_frequency_load_side  = DataUtils.HexToInt(sendMessage.info[131]+sendMessage.info[130]);
                    //54 B相频率（负载侧） Phase B frequency (Load side)
                    int phase_b_frequency_load_side  = DataUtils.HexToInt(sendMessage.info[133]+sendMessage.info[132]);
                    //55 C相频率（负载侧） Phase C frequency (Load side)
                    int phase_c_frequency_load_side  = DataUtils.HexToInt(sendMessage.info[135]+sendMessage.info[134]);


                    //56 A相电流（负载侧） Phase A current (Load side)
                    int phase_a_current_load_side  = DataUtils.HexToInt(sendMessage.info[137]+sendMessage.info[136]);
                    //57 B相电流（负载侧 Phase B current (Load side)
                    int phase_b_current_load_side  = DataUtils.HexToInt(sendMessage.info[139]+sendMessage.info[138]);
                    //58 C相电流（负载侧） Phase C current (Load side)
                    int phase_c_current_load_side  = DataUtils.HexToInt(sendMessage.info[141]+sendMessage.info[140]);



                    //59 A相有功功率（负载侧） Phase A active power (Load side)
                    int  phase_a_active_power_load_side  = DataUtils.HexToInt(sendMessage.info[143]+sendMessage.info[122]);
                    //60 B相有功功率（负载侧） Phase B active power (Load side)
                    int phase_b_active_power_load_side  = DataUtils.HexToInt(sendMessage.info[145]+sendMessage.info[144]);
                    //61 C相有功功率（负载侧） Phase C active power (Load side)
                    int phase_c_active_power_load_side  = DataUtils.HexToInt(sendMessage.info[147]+sendMessage.info[146]);



                    //62 A相无功功率（负载侧） Phase A reactive power (Load side)
                    int phase_a_reactive_power_load_side  = DataUtils.HexToInt(sendMessage.info[149]+sendMessage.info[148]);
                    //63 B相无功功率（负载侧） Phase B reactive power (Load side)
                    int phase_b_reactive_power_load_side  = DataUtils.HexToInt(sendMessage.info[151]+sendMessage.info[150]);
                    //64 C相无功功率（负载侧） Phase C reactive power (Load side)
                    int phase_c_reactive_power_load_side  = DataUtils.HexToInt(sendMessage.info[153]+sendMessage.info[152]);


                    //65 A相视在功率（负载侧） Phase A apparent power (Load side)
                    int phase_a_apparent_power_load_side  = DataUtils.HexToInt(sendMessage.info[155]+sendMessage.info[154]);
                    //66 B相视在功率（负载侧） Phase B apparent power (Load side)
                    int phase_b_apparent_power_load_side  = DataUtils.HexToInt(sendMessage.info[157]+sendMessage.info[156]);
                    //67 C相视在功率（负载侧） Phase C apparent power (Load side)
                    int phase_c_apparent_power_load_side  = DataUtils.HexToInt(sendMessage.info[159]+sendMessage.info[158]);


                    //68 三相有功功率和（负载侧） The sum of three-phase active power (Load side)
                    int the_sum_of_three_phase_active_power_load_side  = DataUtils.HexToInt(sendMessage.info[161]+sendMessage.info[160]);
                    //69 三相无功功率和（负载侧） The sum of three-phase reactive power (Load side)
                    int the_sum_of_three_phase_reactive_power_load_side  = DataUtils.HexToInt(sendMessage.info[163]+sendMessage.info[162]);
                    //70 三相视在功率和（负载侧） The sum of three-phase apparent power (Load side)
                    int the_sum_of_three_phase_apparent_power_load_side  = DataUtils.HexToInt(sendMessage.info[165]+sendMessage.info[164]);


                    //71 直流BUS电压 DC BUS Voltage
                    int dc_bus_voltage  = DataUtils.HexToInt(sendMessage.info[167]+sendMessage.info[166]);
                    //72 直流BUS电流 DC BUS Current
                    int dc_bus_current  = DataUtils.HexToInt(sendMessage.info[169]+sendMessage.info[168]);
                    //73 直流BUS功率 DC BUS Power
                    int dc_bus_power  = DataUtils.HexToInt(sendMessage.info[171]+sendMessage.info[170]);



                    //74 DCAC温度 DC AC Temperature
                    int dc_ac_temperature  = DataUtils.HexToInt(sendMessage.info[173]+sendMessage.info[172]);

                    //75 用电功率 Power Consumption Power
                    int power_consumption_power  = DataUtils.HexToInt(sendMessage.info[177]+sendMessage.info[176]);



                    //76 PV1保护状态 PV1 protection status
                    int pv1_protection_status  = DataUtils.HexToInt(sendMessage.info[180]);
                    //77 PV1工作状态 PV1 working status
                    int pv1_working_status  = DataUtils.HexToInt(sendMessage.info[181]);
                    //78 PV1工作模式 PV1 working mode
                    int pv1_working_mode  = DataUtils.HexToInt(sendMessage.info[182]);




                    //79 PV1输入端电压 PV1 input voltage
                    int pv1_input_voltage  = DataUtils.HexToInt(sendMessage.info[184]+sendMessage.info[183]);
                    //80 PV1输入端电压 PV1 input current
                    int pv1_input_current  = DataUtils.HexToInt(sendMessage.info[186]+sendMessage.info[185]);
                    //81 PV1输入端功率 PV1 input power
                    int pv1_input_power  = DataUtils.HexToInt(sendMessage.info[188]+sendMessage.info[187]);


                    //82 PPV2 input voltage PV2输入端电压
                    int pv2_input_voltage  = DataUtils.HexToInt(sendMessage.info[190]+sendMessage.info[189]);
                    //83 PV2 input current PV2输入端电压
                    int pv2_input_current  = DataUtils.HexToInt(sendMessage.info[192]+sendMessage.info[191]);
                    //84 PV2 input power PV2输入端功率
                    int pv2_input_power  = DataUtils.HexToInt(sendMessage.info[194]+sendMessage.info[193]);


                    //85 Output voltage 输出端电压
                    int output_voltage  = DataUtils.HexToInt(sendMessage.info[196]+sendMessage.info[195]);
                    //86 Output current 输出端电流
                    int output_current  = DataUtils.HexToInt(sendMessage.info[198]+sendMessage.info[197]);
                    //87 Output power 输出端电流
                    int output_power  = DataUtils.HexToInt(sendMessage.info[200]+sendMessage.info[199]);



                    //88 DCDC protection status DCDC保护状态
                    int dcdc_protection_status  = DataUtils.HexToInt(sendMessage.info[201]);
                    //89 DCDC working status DCDC工作状态
                    int dcdc_working_status  = DataUtils.HexToInt(sendMessage.info[202]);
                    //90 DCDC working mode DCDC工作模式
                    int dcdc_working_mode   = DataUtils.HexToInt(sendMessage.info[203]);


                    //91 Battery relay status 电池继电器状态
                    int battery_relay_status  = DataUtils.HexToInt(sendMessage.info[204]);



                    //92 High voltage DC voltage 高压端直流电压
                    int high_voltage_dc_voltage  = DataUtils.HexToInt(sendMessage.info[230]+sendMessage.info[229]);
                    //93 High voltage DC current 高压端直流电流
                    int high_voltage_dc_current  = DataUtils.HexToInt(sendMessage.info[232]+sendMessage.info[231]);
                    //94 High voltage DC power  高压端直流功率
                    int high_voltage_dc_power   = DataUtils.HexToInt(sendMessage.info[234]+sendMessage.info[233]);


                    //92 Low voltage DC voltage 低压端直流电压
                    int low_voltage_dc_voltage  = DataUtils.HexToInt(sendMessage.info[236]+sendMessage.info[235]);
                    //93 Low voltage DC current 低压端直流电流
                    int low_voltage_dc_current  = DataUtils.HexToInt(sendMessage.info[238]+sendMessage.info[237]);
                    //94 Low voltage DC power  低压端直流功率
                    int low_voltage_dc_power   = DataUtils.HexToInt(sendMessage.info[240]+sendMessage.info[239]);


                    //95 DCDC temperature  DCDC温度
                    int dcdc_temperature   = DataUtils.HexToInt(sendMessage.info[242]+sendMessage.info[241]);


                    //96 PV2 protection status PV2工作状态
                    int dpv2_protection_status = DataUtils.HexToInt(sendMessage.info[243]);
                    //97 PV2 working status PV2工作状态
                    int pv2_working_status_pv2  = DataUtils.HexToInt(sendMessage.info[244]);
                    //98 PV2 working mode PV2工作模式
                    int pv2_working_mode_pv2   = DataUtils.HexToInt(sendMessage.info[245]);
                    //99 PV overall working mode PV整体工作模式
                    int pv_overall_working_mode  = DataUtils.HexToInt(sendMessage.info[246]);
                    list.clear();




                    list.add(new WindowDto("PCS Protection status", pcs_protection_status));
                    list.add(new WindowDto("PCS Working Status", pcs_working_status));
                    list.add(new WindowDto("PCS working Mode", pcs_working_mode));
                    list.add(new WindowDto("Grid Status", grid_status));
                    list.add(new WindowDto("Grid Replay Status", grid_replay_status));
                    list.add(new WindowDto("Bypass relay status", bypass_relay_status));
                    list.add(new WindowDto("Load relay status", load_relay_status));
                    //
                    list.add(new WindowDto("Phase A voltage (AC side)", phase_a_voltage));
                    list.add(new WindowDto("Phase B voltage (AC side)", phase_b_voltage));
                    list.add(new WindowDto("Phase C voltage (AC side)", phase_C_voltage));
                    //
                    list.add(new WindowDto("Phase A frequency (AC side)", phase_a_frequency));
                    list.add(new WindowDto("Phase B frequency (AC side)", phase_b_frequency));
                    list.add(new WindowDto("Phase C frequency (AC side)", phase_c_frequency));

                    //
                    list.add(new WindowDto("Phase A current (AC side)", phase_a_current));
                    list.add(new WindowDto("Phase B current (AC side)", phase_b_current));
                    list.add(new WindowDto("Phase C current (AC side)", phase_c_current));
                    //
                    list.add(new WindowDto("Phase A active power (AC side)", phase_a_active_power));
                    list.add(new WindowDto("Phase B active power (AC side)", phase_b_active_power));
                    list.add(new WindowDto("Phase C active power (AC side)", phase_c_active_power));
                    //
                    list.add(new WindowDto("Phase A reactive power (AC side)", phase_a_reactive_power));
                    list.add(new WindowDto("Phase B reactive power (AC side)", phase_b_reactive_power));
                    list.add(new WindowDto("Phase C reactive power (AC side)", phase_c_reactive_power));


                    //
                    list.add(new WindowDto("Phase A apparent power (AC side)", phase_a_apparent_power));
                    list.add(new WindowDto("Phase B apparent power (AC side)", phase_b_apparent_power));
                    list.add(new WindowDto("Phase C apparent power (AC side)", phase_c_apparent_power));

                    //
                    list.add(new WindowDto("The sum of three-phase active power (AC side)", the_sum_of_three_phase_active_power));
                    list.add(new WindowDto("The sum of three-phase reactive power (AC side)", the_sum_of_three_phase_reactive_power));
                    list.add(new WindowDto("The sum of three-phase apparent power (AC side)", the_sum_of_three_phase_apparent_power));



                    //
                    list.add(new WindowDto("Phase A voltage (grid side)", phase_a_voltage_grid_side));
                    list.add(new WindowDto("Phase B voltage (grid side)", phase_b_voltage_grid_side));
                    list.add(new WindowDto("Phase C voltage (grid side)", phase_c_voltage_grid_side));



                    //
                    list.add(new WindowDto("Phase A frequency (grid side)", phase_a_frequency_grid_side));
                    list.add(new WindowDto("Phase B frequency (grid side)", phase_b_frequency_grid_side));
                    list.add(new WindowDto("Phase C frequency (grid side)", phase_c_frequency_grid_side));


                    //
                    list.add(new WindowDto("Phase A current (grid side)", phase_a_current_grid_side));
                    list.add(new WindowDto("Phase B current (grid side)", phase_b_current_grid_side));
                    list.add(new WindowDto("Phase C current (grid side)", phase_c_current_grid_side));

                    //
                    list.add(new WindowDto("Phase A active power (Grid side)", phase_a_active_power_grid_side));
                    list.add(new WindowDto("Phase B active power (Grid side)", phase_b_active_power_grid_side));
                    list.add(new WindowDto("Phase C active power (Grid side)", phase_c_active_power_grid_side));

                    //
                    list.add(new WindowDto("Phase A reactive power (Grid side)", phase_a_reactive_power_grid_side));
                    list.add(new WindowDto("Phase B reactive power (Grid side)", phase_b_reactive_power_grid_side));
                    list.add(new WindowDto("Phase C reactive power (Grid side)", phase_c_reactive_power_grid_side));


                    //
                    list.add(new WindowDto("Phase A apparent power (Grid side)", phase_a_apparent_power_grid_side));
                    list.add(new WindowDto("Phase B apparent power (Grid side)", phase_b_apparent_power_grid_side));
                    list.add(new WindowDto("Phase C apparent power (Grid side)", phase_c_apparent_power_grid_side));


                    //
                    list.add(new WindowDto("The sum of three-phase active power (Grid side)", the_sum_of_three_phase_active_power_grid_side));
                    list.add(new WindowDto("The sum of three-phase reactive power (AC side)", the_sum_of_three_phase_reactive_power_ac_side));
                    list.add(new WindowDto("The sum of three-phase apparent power (AC side)", the_sum_of_three_phase_apparent_power_ac_side));


                    //
                    list.add(new WindowDto("Phase A voltage (Load side)", phase_a_voltage_load_side));
                    list.add(new WindowDto("Phase B voltage (Load side)", phase_b_voltage_load_side));
                    list.add(new WindowDto("Phase C voltage (Load side)", phase_c_voltage_load_side));




                    //
                    list.add(new WindowDto("Phase A frequency (Load side)", phase_a_frequency_load_side));
                    list.add(new WindowDto("Phase B frequency (Load side)", phase_b_frequency_load_side));
                    list.add(new WindowDto("Phase C frequency (Load side)", phase_c_frequency_load_side));



                    //
                    list.add(new WindowDto("Phase A current (Load side)", phase_a_current_load_side));
                    list.add(new WindowDto("Phase B current (Load side)", phase_b_current_load_side));
                    list.add(new WindowDto("Phase C current (Load side)", phase_c_current_load_side));



                    //
                    list.add(new WindowDto("Phase A active power (Load side)", phase_a_active_power_load_side));
                    list.add(new WindowDto("Phase B active power (Load side)", phase_b_active_power_load_side));
                    list.add(new WindowDto("Phase C active power (Load side)", phase_c_active_power_load_side));




                    //
                    list.add(new WindowDto("Phase A reactive power (Load side)", phase_a_reactive_power_load_side));
                    list.add(new WindowDto("Phase B reactive power (Load side)", phase_b_reactive_power_load_side));
                    list.add(new WindowDto("Phase C reactive power (Load side)", phase_c_reactive_power_load_side));


                    //
                    list.add(new WindowDto("Phase A apparent power (Load side)", phase_a_apparent_power_load_side));
                    list.add(new WindowDto("Phase B apparent power (Load side)", phase_b_apparent_power_load_side));
                    list.add(new WindowDto("Phase C apparent power (Load side)", phase_c_apparent_power_load_side));

                    //
                    list.add(new WindowDto("The sum of three-phase active power (Load side)", the_sum_of_three_phase_active_power_load_side));
                    list.add(new WindowDto("The sum of three-phase reactive power (Load side)", the_sum_of_three_phase_reactive_power_load_side));
                    list.add(new WindowDto("The sum of three-phase apparent power (Load side)", the_sum_of_three_phase_apparent_power_load_side));


                    //
                    list.add(new WindowDto("DC BUS Voltage", dc_bus_voltage));
                    list.add(new WindowDto("DC BUS Current", dc_bus_current));
                    list.add(new WindowDto("DC BUS Power", dc_bus_power));


                    //
                    list.add(new WindowDto("DC AC Temperature", dc_ac_temperature));
                    list.add(new WindowDto("Power Consumption Power", power_consumption_power));


                    //
                    list.add(new WindowDto("PV1 protection status", pv1_protection_status));
                    list.add(new WindowDto("PV1 working status", pv1_working_status));
                    list.add(new WindowDto("PV1 working mode", pv1_working_mode));


                    //
                    list.add(new WindowDto("PV1 input voltage", pv1_input_voltage));
                    list.add(new WindowDto("PV1 input current", pv1_input_current));
                    list.add(new WindowDto("PV1 input power", pv1_input_power));



                    //
                    list.add(new WindowDto("PV1 input voltage", pv1_input_voltage));
                    list.add(new WindowDto("PV1 input current", pv1_input_current));
                    list.add(new WindowDto("PV1 input power", pv1_input_power));


                    //
                    list.add(new WindowDto("PV2 input voltage", pv2_input_voltage));
                    list.add(new WindowDto("PV2 input current", pv2_input_current));
                    list.add(new WindowDto("PV2 input power", pv2_input_power));


                    //
                    list.add(new WindowDto("Output voltage", output_voltage));
                    list.add(new WindowDto("Output current", output_current));
                    list.add(new WindowDto("Output powe", output_power));

                    //
                    list.add(new WindowDto("DCDC protection status", dcdc_protection_status));
                    list.add(new WindowDto("DCDC working status", dcdc_working_status));
                    list.add(new WindowDto("DCDC working mode", dcdc_working_mode));

                    //
                    list.add(new WindowDto("Battery relay status", battery_relay_status));

                    //

                    list.add(new WindowDto("High voltage DC voltage", high_voltage_dc_voltage));
                    list.add(new WindowDto("High voltage DC current", high_voltage_dc_current));
                    list.add(new WindowDto("High voltage DC power )", high_voltage_dc_power));

                    //

                    list.add(new WindowDto("Low voltage DC voltage", low_voltage_dc_voltage));
                    list.add(new WindowDto("Low voltage DC current", low_voltage_dc_current));
                    list.add(new WindowDto("Low voltage DC power )", low_voltage_dc_power));

                    //

                    list.add(new WindowDto("DCDC temperature", dcdc_temperature));

                    //
                    list.add(new WindowDto("PV2 protection status PV2", dpv2_protection_status));
                    list.add(new WindowDto("PV2 working status PV2", pv2_working_status_pv2));
                    list.add(new WindowDto("PV2 working mode PV2", pv2_working_mode_pv2));
                    list.add(new WindowDto("PV overall working mode", pv_overall_working_mode));

                    Log.e(TAG, "accept: 刷新了吗-----------" );
                    adapter.notifyDataSetChanged();


                } else if (DataUtils.HexToInt(sendMessage.info[6]) == 0x94) {//EMS信息上传(0x94)
                    Log.e("实时信息0x94", ":" + Arrays.toString(sendMessage.info));
                    Log.e("实时信息0x94", "EMS信息上传(0x94),对应数据长度:" + length + ",总数据长度:" + sendMessage.info.length);
                } else if (DataUtils.HexToInt(sendMessage.info[6]) == 0x95) {//事件信息上传(0x95)
                    Log.e("实时信息0x95", ":" + Arrays.toString(sendMessage.info));
                    Log.e("实时信息0x95", "事件信息上传(0x95),对应数据长度:" + length + ",总数据长度:" + sendMessage.info.length);
                }
            }
        }

        //  查询命令
        if (sendMessage != null && sendMessage.readContent[2] == SocketConfig.SEARCH) {
            Log.e("查询命令", "我接收到了查询命令的所有数据(已过滤传输规则中其他数据):" + Arrays.toString(sendMessage.info));
            Log.e("查询命令", "前六位是年月日时分秒:" + DataUtils.HexToInt(sendMessage.info[0]) + "年" + DataUtils.HexToInt(sendMessage.info[1]) + "月" + DataUtils.HexToInt(sendMessage.info[2]) + "日" + DataUtils.HexToInt(sendMessage.info[3]) + "时" + DataUtils.HexToInt(sendMessage.info[4]) + "分" + DataUtils.HexToInt(sendMessage.info[5]) + "秒");

        } else if (sendMessage != null) {
            Log.e("设置信息", "我接收到了实时信息的所有数据(已过滤传输规则中其他数据):" + Arrays.toString(sendMessage.info));

        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (sendMessage != null && sendMessage.readContent[3] == (byte) 0xFE) {
                    Log.e("浪", "为了保持app的Socket通信，设备基本会实时发送数据，在应答标志为FE时就发送一次消息，确认信息获取成功");
                    SocketUtils.sendSerialPort(SocketConfig.settingVehicleAccess(sendMessage.readContent));
                }
            }
        }).start();
    }




}


